package com.bum.guru.dj.bo;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.bum.guru.exp.dj.IDjGuruService;
import com.bum.guru.exp.dj.om.Song;
import com.bum.guru.util.BaseParser;
import com.mg.me.data.grid.IGridMap;

public class DjListBO {
	IGridMap	djGridMap;

	private List<Song> list(String url) {
		if (StringUtils.isBlank(url)) {
			return null;
		}

		String html = BaseParser.loadHtml(url);
		if (StringUtils.isBlank(html)) {
			return null;
		}
		html = html.replace("\"", "`");
		html = BaseParser.parseContent(html, "<div class=`block block_1`> <div class=`title_block`>", "<div class=`view_more` id=`page`>");

		List<Song> songs = new LinkedList<Song>();

		String[] parts = html.split("<p class='name_1'><a");
		for (int i = 1; i < parts.length; i++) {
			String item = parts[i];

			String sId = BaseParser.parseContent(item, "/nhac-san/", "/");
			if (StringUtils.isBlank(sId)) {
				continue;
			}
			long id = NumberUtils.toLong(sId, 0);
			if (id <= 0) {
				continue;
			}

			String name = BaseParser.parseContent(item, "'>", "</a>");
			String listenCount = BaseParser.parseContent(item, "<i class='icon-headphones'></i> Lượt nghe: ", "</span>");

//			String user = BaseParser.parseContent(item, "<i class='icon-user'></i> ", "</a>");
//			String cat = BaseParser.parseContent(item, "<i class='icon-music'></i> ", "</span>");

			Song song = new Song();
			song.setId(id);
			song.setName(name);
			song.setNoHits(NumberUtils.toInt(listenCount.replace(",", ""), 0));

			songs.add(song);
		}

		return songs;
	}

	static Map<Integer, String>	viewUrl	= new HashMap<Integer, String>();
	static {
		viewUrl.put(IDjGuruService.VIEW_TOP_HIT_BY_MONTH, "http://m.matxac.com/nhac-san/nghe-nhieu-nhat-trong-thang.html");
		viewUrl.put(IDjGuruService.VIEW_TOP_HIT_BY_WEEK, "http://m.matxac.com/nhac-san/nghe-nhieu-nhat-trong-tuan.html");
		viewUrl.put(IDjGuruService.VIEW_TOP_HIT_BY_DAY, "http://m.matxac.com/nhac-san/nghe-nhieu-nhat-trong-ngay.html");
		viewUrl.put(IDjGuruService.VIEW_TOP_VOTE_BY_WEEK, "http://m.matxac.com/nhac-san/binh-chon-trong-tuan.html");
		viewUrl.put(IDjGuruService.VIEW_TOP_DOWN_BY_WEEK, "http://m.matxac.com/nhac-san/download-nhieu-trong-tuan.html");
	}

	public List<Song> list(int viewId) {
		String url = viewUrl.get(viewId);
		if (StringUtils.isBlank(url)) {
			return null;
		}

		String key = "top#" + viewId;
		List<Song> list = (List<Song>) djGridMap.get(key);
		if (list != null) {
			return list;
		}

		list = list(url);
		djGridMap.put(key, list);

		return list;
	}

	public static void main(String[] args) {
		DjListBO bo = new DjListBO();
		bo.list("http://m.matxac.com/nhac-san/nghe-nhieu-nhat-trong-ngay.html");
	}

	public void setDjGridMap(IGridMap djGridMap) {
		this.djGridMap = djGridMap;
	}
}
